package cn.zcscloud.mall.service.impl;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.mbg.mapper.PmsMemberPriceMapper;
import cn.zcscloud.mall.mbg.mapper.PmsProductMapper;
import cn.zcscloud.mall.mbg.mapper.UmsAdminMapper;
import cn.zcscloud.mall.mbg.model.*;
import cn.zcscloud.mall.service.*;
import cn.zcscloud.mall.vo.CreateVo;
import cn.zcscloud.mall.vo.PmsProductResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class PmsProductServiceImpl extends ServiceImpl<PmsProductMapper, PmsProduct> implements PmsProductService {
    @Autowired
    private PmsProductMapper pmsProductMapper;
    @Autowired
    private PmsMemberPriceService memberPriceService;
    @Autowired
    private CmsPrefrenceAreaProductRelationService prefrenceAreaProductRelationService;
    @Autowired
    private PmsProductFullReductionService productFullReductionService;
    @Autowired
    private CmsSubjectProductRelationService subjectProductRelationService;
    @Autowired
    private PmsProductLadderService productLadderService;
    @Autowired
    private PmsProductAttributeValueService productAttributeValueService;
    @Autowired
    private PmsSkuStockService skuStockService;
    @Autowired
    private PmsProductCategoryService productCategoryService;

    private static final Integer UPDATE = 1;
    private static final Integer REMOVE = 0;

    @Override
    public Boolean updateByIds(PmsProduct pmsProduct, LambdaQueryWrapper<PmsProduct> lqw) {
        int update = pmsProductMapper.update(pmsProduct, lqw);
        log.info("成功修改{}条数据",update);
        return true;
    }

    @Override
    public Boolean insert(CreateVo createVo,Integer code) throws InvocationTargetException, IllegalAccessException {
        //创建商品
        PmsProduct pmsProduct = createVo;
        if (code != UPDATE){
            this.save(pmsProduct);  //保存基本数据
        }
        ArrayList<PmsMemberPrice> memberPriceList = createVo.getMemberPriceList();
        ArrayList<CmsPrefrenceAreaProductRelation> reductionList = createVo.getPrefrenceAreaProductRelationList();
        ArrayList<PmsProductFullReduction> productFullReductionList = createVo.getProductFullReductionList();
        ArrayList<CmsSubjectProductRelation> cmsSubjectProductRelationList = createVo.getCmsSubjectProductRelationList();
        ArrayList<PmsProductLadder> productLadderList = createVo.getProductLadderList();
        ArrayList<PmsProductAttributeValue> productAttributeValueArrayList = createVo.getProductAttributeValueArrayList();
        ArrayList<PmsSkuStock> skuStockList = createVo.getSkuStockList();
        //保存会员价格
        if (memberPriceList!=null&&!memberPriceList.isEmpty()) {
            for (PmsMemberPrice pmsMemberPrice : memberPriceList) {
                pmsMemberPrice.setProductId(pmsProduct.getId());
            }
            memberPriceService.saveBatch(memberPriceList);
        }
        if (reductionList!=null&&!reductionList.isEmpty()) {
            //关联优选
            for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : reductionList) {
                cmsPrefrenceAreaProductRelation.setProductId(pmsProduct.getId());
            }
            prefrenceAreaProductRelationService.saveBatch(reductionList);
        }
        //商品满减价格关联
        if (productFullReductionList != null && !productFullReductionList.isEmpty()) {
            for (PmsProductFullReduction fullReduction : productFullReductionList) {
                fullReduction.setProductId(pmsProduct.getId());
            }
            productFullReductionService.saveBatch(productFullReductionList);
        }
        //专题和商品关系
        if (cmsSubjectProductRelationList!=null&&!cmsSubjectProductRelationList.isEmpty()) {
            for (CmsSubjectProductRelation cmsSubjectProductRelation : cmsSubjectProductRelationList) {
                cmsSubjectProductRelation.setProductId(pmsProduct.getId());
            }
            log.info("cmsSubjectProductRelationList === {}",cmsSubjectProductRelationList);
            subjectProductRelationService.saveBatch(cmsSubjectProductRelationList);
        }
        if (productLadderList!=null&&!productLadderList.isEmpty()) {
            //阶梯价格
            for (PmsProductLadder pmsProductLadder : productLadderList) {
                pmsProductLadder.setProductId(pmsProduct.getId());
            }
            productLadderService.saveBatch(productLadderList);
        }
        if (productAttributeValueArrayList!=null&&!productAttributeValueArrayList.isEmpty()) {
            //商品参数及自定义规格属性
            for (PmsProductAttributeValue attributeValue : productAttributeValueArrayList) {
                attributeValue.setProductId(pmsProduct.getId());
            }
            productAttributeValueService.saveBatch(productAttributeValueArrayList);
        }
        if (skuStockList!=null&&!skuStockList.isEmpty()) {
            //商品的sku库存信息
            for (PmsSkuStock pmsSkuStock : skuStockList) {
                pmsSkuStock.setProductId(pmsProduct.getId());
            }
            skuStockService.saveBatch(skuStockList);
        }
        log.info("===== ");
        return null;
    }

    @Override
    public Boolean changeById(Long id, CreateVo createVo) throws InvocationTargetException, IllegalAccessException {
        //移除
        ArrayList<Long> lists = new ArrayList<>();
        lists.add(id);
        this.deleteByIds(lists,UPDATE);

        //添加
        this.insert(createVo,UPDATE);
        //更新商品信息
        /*PmsProduct product = createVo;
        this.updateById(product);
        ArrayList<PmsMemberPrice> memberPriceList = createVo.getMemberPriceList();
        ArrayList<CmsPrefrenceAreaProductRelation> reductionList = createVo.getPrefrenceAreaProductRelationList();
        ArrayList<PmsProductFullReduction> productFullReductionList = createVo.getProductFullReductionList();
        ArrayList<CmsSubjectProductRelation> cmsSubjectProductRelationList = createVo.getCmsSubjectProductRelationList();
        ArrayList<PmsProductLadder> productLadderList = createVo.getProductLadderList();
        ArrayList<PmsProductAttributeValue> productAttributeValueArrayList = createVo.getProductAttributeValueArrayList();
        ArrayList<PmsSkuStock> skuStockList = createVo.getSkuStockList();
        //保存会员价格
        memberPriceService.updateBatchById(memberPriceList);
        //关联优选
        prefrenceAreaProductRelationService.updateBatchById(reductionList);
        //商品满减价格关联
        productFullReductionService.updateBatchById(productFullReductionList);
        //专题和商品关系
        subjectProductRelationService.updateBatchById(cmsSubjectProductRelationList);
        //阶梯价格
        productLadderService.updateBatchById(productLadderList);
        //商品参数及自定义规格属性
        productAttributeValueService.updateBatchById(productAttributeValueArrayList);
        //商品的sku库存信息
        skuStockService.updateBatchById(skuStockList);*/

        return true;
    }

    @Override
    public Boolean deleteByIds(List ids,Integer code) {
        //商品的sku库存信息
        QueryWrapper qw = new QueryWrapper();
        qw.in("product_id",ids);
        //商品的sku库存信息
        boolean remove = skuStockService.remove(qw);
        //商品参数及自定义规格属性
        boolean remove1 = productAttributeValueService.remove(qw);
        //阶梯价格
        boolean remove2 = productLadderService.remove(qw);
        //保存会员价格
        boolean remove3 = memberPriceService.remove(qw);
        //关联优选
        boolean remove4 = prefrenceAreaProductRelationService.remove(qw);
        //商品满减价格关联
        boolean remove5 = productFullReductionService.remove(qw);
        //专题和商品关系
        boolean remove6 = subjectProductRelationService.remove(qw);
        if (code != UPDATE){
            this.removeByIds(ids);
        }
        return true;
    }

    @Override
    public PmsProductResult seleteById(Long id) throws InvocationTargetException, IllegalAccessException {
        PmsProductResult pmsProductResult = new PmsProductResult();
        PmsProduct pmsProduct = this.getById(id);
        BeanUtilsBean.getInstance().getConvertUtils().register(false, false, 0);
        ConvertUtils.register(new DateConverter(null), java.util.Date.class);
        BeanUtils.copyProperties(pmsProduct,pmsProductResult);

        QueryWrapper qw = new QueryWrapper();
        qw.in("product_id",id);
        //商品的sku库存信息
         //skuStockService.list(qw);
        pmsProductResult.setSkuStockList((ArrayList<PmsSkuStock>) skuStockService.list(qw));
        //商品参数及自定义规格属性
         //productAttributeValueService.list(qw);
        pmsProductResult.setProductAttributeValueArrayList((ArrayList<PmsProductAttributeValue>) productAttributeValueService.list(qw));
        //阶梯价格
         //productLadderService.list(qw);
        pmsProductResult.setProductLadderList((ArrayList<PmsProductLadder>) productLadderService.list(qw));
        //保存会员价格
         //memberPriceService.list(qw);
        pmsProductResult.setMemberPriceList((ArrayList<PmsMemberPrice>) memberPriceService.list(qw));
        //关联优选
         //prefrenceAreaProductRelationService.list(qw);
         pmsProductResult.setPrefrenceAreaProductRelationList((ArrayList<CmsPrefrenceAreaProductRelation>) prefrenceAreaProductRelationService.list(qw));
        //商品满减价格关联
         //productFullReductionService.list(qw);
         pmsProductResult.setProductFullReductionList((ArrayList<PmsProductFullReduction>) productFullReductionService.list(qw));
        //专题和商品关系
         //subjectProductRelationService.list(qw);
         pmsProductResult.setCmsSubjectProductRelationList((ArrayList<CmsSubjectProductRelation>) subjectProductRelationService.list(qw));
        //return pmsProductMapper.seleteById(id);
        Long parentId = productCategoryService.getById(pmsProductResult.getProductCategoryId()).getParentId();
        log.error("parentId ======================== {}",parentId);
        pmsProductResult.setCateParentId(parentId);
        return pmsProductResult;
    }


}
